<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Log
 *
 * @author bonfiglio
 */
class Log {

    const key = '.certinergy.';

    public static function fputs($fileNamePath, $content) {
        $file = fopen($fileNamePath, "a+");
        if (!$file) {
            return false;
        }
        fputs($file, $content);
        fclose($file);
        return true;
    }

    public static function write($fileNamePath, $message, $title = false) {
        if (!is_scalar($message))
            $message = print_r($message, true);
        try {
            $file = @fopen($fileNamePath, "a+");
            if ($file) {
                if ($title && strlen($title)) {
                    @fputs($file, "************************" . $title . "************************\n");
                }
                @fputs($file, $message);
                @fclose($file);
            } else {
                error_log('LOG - unable to open/write file ' . $fileNamePath . ' with this message  (' . $message . ')');
            }
        } catch (Exception $e) {
            error_log($e->getMessage());
        }
    }

    public static function sql($message) {
        $message .= "\n";
        self::write(ENGINE_ROOT . '/log/sql' . self::key . strftime('%Y-%m-%d') . '.log', $message);
    }

    public static function crawler($message, $mode = 'cron', $level = 'log') {
        $message .= "\n";
        self::write(ENGINE_ROOT . '/log/crawler' . self::key . strftime('%Y-%m-%d') . '.log', '[' . $mode . '] [' . strftime('%c') . '] [' . $level . '] ' . $message);
    }

    public static function l($message) {
        $message .= "\n";
        self::write(ENGINE_ROOT . '/log/log' . self::key . strftime('%Y-%m-%d') . '.log', $message);
    }

    public static function tr($b = null) {
        self::pr(debug_backtrace(), $b);
    }

    public static function tre($b = null) {
        self::pre(debug_backtrace(), $b);
    }

    public static function pr($a, $b = null, $color = '#F5F5F5', $return = false) {
        $b = print_r($b,true);
        $b = strlen($b) ? '<h4>' . $b . '</h4>' : '';
        $log = '<pre 
            class="cer-debug"
            style=" background-color: ' . $color . ';border: 1px solid rgba(0, 0, 0, 0.05);border-radius: 4px 4px 4px 4px;
                    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05) inset;margin-bottom: 20px;min-height: 20px;padding: 19px;">' . $b . print_r($a, true) . '</pre>';
        if (!$return) {
            echo $log;
        }
        return $log;
    }

    public static function pre($a=null, $b = null, $c = '#F5F5F5') {
        self::pr($a, $b, $c);
        exit;
    }

}

?>
